package cn.chuanw.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.chuanw.wx.annotation.RequiresPermissionsDesc;
import cn.chuanw.core.util.RandomNumberUtil;
import cn.chuanw.core.util.ResponseUtil;
import cn.chuanw.db.domain.WHousepayRecord;
import cn.chuanw.db.service.WHousepayRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/housepay")
@Validated
public class WHousepayRecordController {
    private final Log logger = LogFactory.getLog(WHousepayRecordController.class);

    @Autowired
    private WHousepayRecordService wHousepayRecordService;

    @RequiresPermissions("admin:housepay:list")
    @RequiresPermissionsDesc(menu = {"公寓管理", "水电支付信息"}, button = "查询")
    @GetMapping("/list")
    public Object list(String houseId,
    		String costType,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        List<WHousepayRecord> wHouseInfos = wHousepayRecordService.querySelective(houseId,costType, page, limit);
        return ResponseUtil.okList(wHouseInfos);
    }


    @RequiresPermissions("admin:housepay:add")
    @RequiresPermissionsDesc(menu = {"公寓管理", "水电支付信息"}, button = "新增")
    @PostMapping("/add")
    public Object add(@RequestBody WHousepayRecord wHousepayRecord) {
    	wHousepayRecord.setId(RandomNumberUtil.getStringRandom(12));
    	wHousepayRecordService.addWHouseInfo(wHousepayRecord);
        return ResponseUtil.ok(wHousepayRecord);
    }


    @RequiresPermissions("admin:housepay:update")
    @RequiresPermissionsDesc(menu = {"公寓管理", "水电支付信息"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody WHousepayRecord wHousepayRecord) {
    	wHousepayRecordService.updateById(wHousepayRecord);
        return ResponseUtil.ok(wHousepayRecord);
    }
    @RequiresPermissions("admaddin:payRentRecord:archiveIncomeRent")
    @RequiresPermissionsDesc(menu = {"公寓管理", "收租记录"}, button = "归档")
    @PostMapping("/archiveIncomeRent")
    public Object archiveIncomeRent(@RequestParam(value = "ids", required = false) String ids) {
    	wHousepayRecordService.archiveIncomeRent(ids);
        return ResponseUtil.ok();
    }

    /**
     * @Auther: wangkun
     * @Date: 20-03-24 上午 9:46
     * @Description: 计算当前收租金额，归档收租金额
     **/
    @GetMapping("/tatolIncomeRent")
    public Object tatolIncomeRent() {
        Object o = wHousepayRecordService.tatolIncomeRent();
        return ResponseUtil.ok(o);
    }
}
